package cn.edu.lzu.unitweb.controller;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.entity.ExamineItem;
import cn.edu.lzu.entity.ExaminePackage;
import cn.edu.lzu.entity.Examiner;
import cn.edu.lzu.service.ExaminePackageService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @PackageName: cn.edu.lzu.unitweb.controller
 * @ClassName: ExaminePackageController
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2019/12/10 14:50
 * @Version: 1.0
 */
@Controller
public class ExaminePackageController {
    @Resource
    private ExaminePackageService examinePackageService;

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取一页套餐数据
     * @Date: 2019/12/10 19:41
     * @Param: [currentPageNum, pageSize]
     * @Return: cn.edu.lzu.common.assist.DataPage
     **/
    @ResponseBody
    @RequestMapping("/getUnitPackageList")
    public DataPage getUnitPackageList(int currentPageNum, int pageSize){
        Session session= SecurityUtils.getSubject().getSession();
        Examiner examiner;
        examiner = (Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        DataPage packagePage=examinePackageService.selectPageExaminePackageByUnitId(examiner.getUnitId(), currentPageNum, pageSize);
        return packagePage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据套餐编号进行单个删除
     * @Date: 2019/12/10 16:23
     * @Param: [examinePackageId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/deleteExaminePackageById")
    public ResponseResult deleteExaminePackageById(int examinePackageId){
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        ResponseResult responseResult=examinePackageService.deleteExaminePackageByUnitId(examinePackageId, examiner.getUnitId());
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 批量删除套餐
     * @Date: 2019/12/10 19:40
     * @Param: [examinePackageIds]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/batchDeleteExaminePackagesByIds")
    public ResponseResult batchDeleteExaminePackagesByIds(String examinePackageIds){
        examinePackageIds=examinePackageIds.substring(0, examinePackageIds.length()-1);
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        ResponseResult responseResult=examinePackageService.batchDeleteExaminePackagesByIds(examinePackageIds, examiner.getUnitId());
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 执行添加套餐的逻辑
     * @Date: 2020/2/26 20:32
     * @Param: [examinePackage, itemsId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/executeAddExaminePackage")
    public ResponseResult executeAddExaminePackage(ExaminePackage examinePackage, String labItems, String radItems, String supItems, String speItems, String medItems, String cliItems){
        System.out.println(examinePackage+"\n"+labItems+"\t"+radItems+"\t"+speItems+"\t"+supItems+"\t"+medItems+"\t"+cliItems);
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        examinePackage.setUnitId(examiner.getUnitId());
        ResponseResult responseResult=examinePackageService.addExaminePackage(examinePackage,labItems, radItems, supItems, speItems, medItems, cliItems);
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("/getUnitPackageListBySex")
    public List<ExaminePackage> getUnitPackageListBySex(String sex){
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner=(Examiner)((PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        ExaminePackage examinePackage=new ExaminePackage();
        examinePackage.setUnitId(examiner.getUnitId());
        //获取不限性别和男性套餐
        if(sex.equals("1")){
            //此处填写相反性别，从而在mapper中使用！获取其余两个使用范围
            examinePackage.setApplySex("女性");
        }else{
        //获取不限性别和女性套餐
            examinePackage.setApplySex("男性");
        }
        List<ExaminePackage> examinePackageList=examinePackageService.selectUnitPackageListBySex(examinePackage);
        System.out.println("获取的套餐列表\n"+examinePackageList);
        return examinePackageList;
    }

}
